package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.CodeTable;
import org.kl.bf.entity.basic.CodeValues;
import org.kl.bf.repository.basic.CodeTableDao;
import org.kl.bf.repository.basic.CodeValuesDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class CodeValuesService {

	private CodeValuesDao codeValuesDao;
	private CodeTableDao codeTableDao;
	
	//private CodeValuesService codevaluesService;

	public CodeValues getCodeValues(String guid) {
		return codeValuesDao.findOne(guid);
	}
	
	public List<CodeValues> getCodeValues(){
		return codeValuesDao.findAll();
	}

	public CodeValues findByTableCodeAndCodeName(String tableCode, String codeName) {
		CodeTable ct = codeTableDao.findByTableCode(tableCode);
		if (ct != null) {
			List<CodeValues> cvList = codeValuesDao.findByCodeTableIdAndCodeName(ct.getId(), codeName);
			if (!Collections3.isEmpty(cvList)) {
				return cvList.get(0);
			}
		}
		return null;
	}

	public void saveCodeValues(CodeValues entity) {
		codeValuesDao.save(entity);
	}

	public List<CodeValues> getCodeValuesByCodeParentId(String codeParentId){
		return codeValuesDao.findByCodeParentId(codeParentId);
	}
	
	public String getCodeNameByCodeValue(String codeValue) {
		CodeValues cv = codeValuesDao.findByCodeValue(codeValue);
		return cv == null ? null : cv.getCodeName();
	}
	
	public List<CodeValues> getCodeValuesByCodeTableId(String codeTableId){
		return codeValuesDao.findByCodeTableId(codeTableId);
	}

	public int deleteCodeValues(String ids) {
		int i = 0;
		String[] idArray = StringUtils.split(ids, ",");
		for (String id : idArray) {
			System.out.println("前台传递ID号："+id);
			if (StringUtils.isNotBlank(id)) {
				List<CodeValues> cvs = codeValuesDao.findByCodeParentId(id);//找到该节点下的所有子节点
				if(cvs!=null){
					for(int j=0;j<cvs.size();j++){
						System.out.println("叶子节点："+cvs.get(j).getCodeName());
						CodeValues codevalues = cvs.get(j) ;
						codevalues.setCodeParentId(" ");
						codeValuesDao.save(codevalues);
						//codeValuesDao.delete(codevalues.getId());
						deleteCodeValues(cvs.get(j).getId());
					}
				}
				CodeValues cv = codeValuesDao.find(CodeValues.class, id);
				if(cv!=null){
					codeValuesDao.delete(id);
					i++;
				}
				//System.out.println("删除的节点为："+cv.getCodeName());
				
			}
			
		}
		return i;
	}

	public List<CodeValues> getAllCodeValues(Map<String, Object> searchParams, String sortType, String orderType) {
		Sort sort = CRUDUtils.buildSort(sortType, orderType);
		Specification<CodeValues> spec = buildSpecification(searchParams);
		List<CodeValues> result = codeValuesDao.findAll(spec, sort);
		for (CodeValues one : result) {
			if (StringUtils.isBlank(one.getCodeParentId())) {
				continue;
			}
			CodeValues parent = codeValuesDao.findOne(one.getCodeParentId());
			one.setParentCodeName(parent == null ? "" : parent.getCodeName());
		}
		return result;
	}

	public List<CodeValues> findByTableCode(String tableCode) {
		CodeTable ct = codeTableDao.findByTableCode(tableCode);
		if (ct != null) {
			return findByTableId(ct.getId(), 0);
		} else {
			return null;
		}
	}

	/**
	 * 获取下拉列表 仅限于2级列表，多级子菜单制定级别，请自定义方法。
	 * 
	 * @param tableId
	 * @param level
	 *            0：全部 1：根节点 2：所有子节点
	 * @return
	 */
	public List<CodeValues> findByTableId(String tableId, int level) {
		List<CodeValues> result;
		switch (level) {
		case 1:
			result = codeValuesDao.findByCodeTableIdAndCodeParentIdIsNull(tableId);
			break;
		case 2:
			result = codeValuesDao.findByCodeTableIdAndCodeParentIdNotNull(tableId);
			break;
		default:
			result = codeValuesDao.findByCodeTableId(tableId);
			break;
		}
		for (CodeValues one : result) {
			if (StringUtils.isNotBlank(one.getCodeParentId())) {
				CodeValues parent = codeValuesDao.findOne(one.getCodeParentId());
				one.setParentCodeName(parent == null ? "" : parent.getCodeName());
			}
		}
		return result;
	}

	public Page<CodeValues> getUserCodeValues(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<CodeValues> spec = buildSpecification(searchParams);

		return codeValuesDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CodeValues> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<CodeValues> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), CodeValues.class);
		return spec;
	}

	@Autowired
	public void setCodeValuesDao(CodeValuesDao codeValuesDao) {
		this.codeValuesDao = codeValuesDao;
	}

	@Autowired
	public void setCodeTableDao(CodeTableDao codeTableDao) {
		this.codeTableDao = codeTableDao;
	}

	public List<CodeValues> getAll(Map<String, Object> searchParams,String sortType, String orderType) {
		Specification<CodeValues> spec = KLDynamicSpecifications.buildSpecification(searchParams, CodeValues.class);
		return codeValuesDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}

}
